package com.starry.core.tenant.core.aop;

import com.starry.core.common.tenant.context.TenantContextHolder;
import com.starry.core.redis.service.RedisService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;


/**
 * 忽略多租户的 Aspect，基于 {@link TenantIgnore} 注解实现，用于一些全局的逻辑。
 *
 * @Author xiaoke
 */
@Aspect
public class TenantIgnoreAspect {

    @Around(value = "@annotation(com.starry.core.tenant.core.aop.TenantIgnore)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Boolean oldIgnore = TenantContextHolder.isIgnore();
        Boolean orldIgnoreCache = RedisService.IGNORE_CACHE.get();
        // Redis缓存忽略
        try {
            TenantContextHolder.setIgnore(true);
            RedisService.IGNORE_CACHE.set(true);
            // 执行逻辑
            return joinPoint.proceed();
        } finally {
            TenantContextHolder.setIgnore(oldIgnore);
            RedisService.IGNORE_CACHE.set(orldIgnoreCache);
        }
    }
}
